/*************************************************************************
	> File Name: main.c
	> Author: QiyangXia
	> Mail: 834061456@qq.com 
	> Created Time: 2021年12月31日 星期五 17时03分38秒
 ************************************************************************/
#include "main.h"
char *mygets(char *pout,int size);
void proc();
int main(int argc, const char **argv)
{
	pid_t pid =-1;
	int max=0;
	struct linklist *plls=NULL;

	if(argc < 2)
	{
		printf("The argument is too few,Usage:./a.out 50:\n");
		return 1;
	}
	pid=fork();
	if(pid<0)
	{
		printf("fork failed\n");
		return 1;
	}else if(pid==0){
		proc();
		exit(0);
	}else{
		wait(NULL);
		sscanf(argv[1],"%d",&max);
    	plls=create_linklist();
		mainloop(plls,max);
		destroy_linklist(plls);
		plls=NULL;
	}

	return 0;
}


int mainloop(struct linklist *plls,int max)
{
	int op=0;
	int exitflag=0;
	struct node *pnew = NULL;
	struct node *ptemp = NULL;
	char tname[NAME_LEN]="";
	T t;
	while(1)
	{
		op=ui();
		switch(op)
		{
		case 1:
			printf("\n\n-------------【插入】-------------\n");
			if(max==plls->len)
			{
				printf("这间教室满人了，不能加入更多学生了\n");
			}else{
				printf("请输入学号，姓名，性别，数学成绩，英语成绩，中文成绩：");
				scanf("%d",&t.no);
				while(getchar()!='\n'){}
				mygets(t.name,NAME_LEN);
				t.sex=getchar();
				while(getchar()!='\n'){}
				scanf("%f",&t.math);
				while(getchar()!='\n'){}
				scanf("%f",&t.english);
				while(getchar()!='\n'){}
				scanf("%f",&t.chinese);
				while(getchar()!='\n'){}
				ptemp=search_by_no(plls,&t.no);
				if(ptemp==NULL)
				{
					pnew=create_node(&t);
					insert_at_tail(plls,pnew);
					printf("学生以成功录入系统\n");
				}else{
					printf("此学号已有学生使用，录入失败\n");
				}
			}
			break;
		case 2:
			printf("\n\n-------------【删除】-------------\n");

			printf("请输入要删除的人名:");   
			scanf("%s",tname);    
			while(getchar()!='\n'){} 
			ptemp=search_by_name(plls,tname);
			if(NULL==ptemp)
			{
				printf("查无此人\n");
			}else{
				remove_a_node(plls,ptemp);
				printf("该学生信息已删除\n");
			}
			break;
		case 3:
			printf("\n\n-------------【查询】-------------\n");
			printf("请输入要查找的学生姓名:");
			scanf("%s",tname);    
			while(getchar()!='\n'){}
			ptemp=search_by_name(plls,tname);
			if(NULL==ptemp)
			{
				printf("查无此人\n");
			}else{
				printf("已找到\n");
				print_a_person(ptemp);
			}
			break;
		case 4:
			printf("\n\n-------------【修改】-------------\n");
			printf("请输入要修改的学生姓名:");
			scanf("%s",tname);    
			while(getchar()!='\n'){}
			ptemp=search_by_name(plls,tname);
			if(NULL==ptemp)
			{
				printf("查无此人\n");
			}else{
				printf("请重新输入此人全部信息\n");
				scanf("%d",&t.no);
				while(getchar()!='\n'){}
				mygets(t.name,NAME_LEN);
				t.sex=getchar();
				while(getchar()!='\n'){}
				scanf("%f",&t.math);
				while(getchar()!='\n'){}
				scanf("%f",&t.english);
				while(getchar()!='\n'){}
				scanf("%f",&t.chinese);
				while(getchar()!='\n'){}
				pnew=search_by_no(plls,&t.no);
				if(NULL==pnew)
				{
					modify(ptemp,&t);
					printf("学生信息已更新\n");
					print_a_person(ptemp);
				}else{
					printf("此学号已有学生使用，录入失败\n");
				}
			}
			break;
		case 5:
			printf("\n\n-----------【总成绩单】-----------\n");
			printf("\n\n----------【快速排序法】----------\n");
			struct node **pall=NULL;
			pall=sort_person_linklist_by_all(plls);
			print_sort_result(pall,get_linklist_len(plls));
			free_sort_result(pall);
			break;
		case 6:
			printf("\n\n-----------【数学成绩单】----------\n");
			printf("\n\n-----------【插入排序法】----------\n");
			struct node **pm=NULL;
			pm=sort_person_linklist_by_math(plls);
			print_sort_result(pm,get_linklist_len(plls));
			free_sort_result(pm);
			break;
		case 7:
			printf("\n\n-----------【英语成绩单】----------\n");
			printf("\n\n-----------【选择排序法】----------\n");
			struct node **pe=NULL;
			pe=sort_person_linklist_by_english(plls);
			print_sort_result(pe,get_linklist_len(plls));
			free_sort_result(pe);
			break;
		case 8:
			printf("\n\n-----------【中文成绩单】----------\n");
			printf("\n\n-----------【冒泡排序法】----------\n");
			struct node **pc=NULL;
			pc=sort_person_linklist_by_chinese(plls);
			print_sort_result(pc,get_linklist_len(plls));
			free_sort_result(pc);
			break;
		case 9:
			printf("\n\n-------------【浏览】-------------\n");
			print_all_person(plls);
			break;
		case 10:
			printf("\n\n-------------【导出】-------------\n");
			write_in(plls);
			break;
		case 11:
			printf("\n\n-------------【录入】-------------\n");
			read_in(plls);
			break;
		case 0:
			printf("********************************************\n");
			printf("********************************************\n");
			printf("********************************************\n");
			printf("\t\t再见\n");
			printf("********************************************\n");
			printf("********************************************\n");
			printf("********************************************\n");
			exitflag=1;
			break;
		}
		if(exitflag)
		{
			break;
		}
	}
	return 0;
}

void proc()
{
	int count=0;
	char buf[N+2];
	memset(buf,'\0',sizeof(buf));
	char index[5]="|/-\\";
	while(count<=N)
	{
		buf[count]='#';
		printf("[%-100s][%3d%%]%c\r",buf,count,index[count%4]);
		fflush(stdout);
		usleep(10000);
		count++;
	}
	printf("\n");
}
